<?php

namespace App\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Cache;

class EmailCode extends Notification
{
    use Queueable;

    public $code;

    public function __construct()
    {
        $this->code = rand(111111, 999999);
    }

    public function via($notifiable)
    {
        return ['mail'];
    }

    public function toMail($notifiable)
    {
        $email = Arr::first($notifiable->routes);
        Cache::put('email_code:'.$email, $this->code, now()->addMinutes(15));

        return (new MailMessage())
            ->greeting('验证码')
            ->line($this->code);
    }
}
